package cn.stylefeng.guns.sys.modular.system.db2.controller;


import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.db2.model.params.BusinessParam;
import cn.stylefeng.guns.sys.modular.system.db2.service.BusinessService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 营业情况
 *
 * @author jiangge
 * @date 2020/10/28 13:38
 */
@Controller
@RequestMapping("/business")
public class BusinessController extends BaseController {

    private String PREFIX = "/modular/system/business/";


    @Autowired
    private BusinessService businessService;

    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-10-27
     */
    @RequestMapping("")
    public String index(Model model) {
        //初始化时间数据，今天4点到明天4点
        String today4Oclock = DateUtil.getToday4Oclock();
        String nextDay4Oclock = DateUtil.getNextDay4Oclock();
        String timecell = today4Oclock + " - " + nextDay4Oclock;
        model.addAttribute("timecell", timecell);
        return PREFIX + "business.html";
    }


    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-10-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BusinessParam businessParam) {

        //日期维度-默认按天
        if (StringUtils.isEmpty(businessParam.getDateDimension())) {
            businessParam.setDateDimension("day");
        }

        if (businessParam.getDateDimension().equals("day")) {
            //时间条件初始化
            if (StringUtils.isEmpty(businessParam.getRangeDate())) {
                //今天04:00:00
                String today4Oclock = DateUtil.getToday4Oclock();
                businessParam.setStartTime(today4Oclock);
                //明天04:00:00
                String next4Oclock = DateUtil.getNextDay4Oclock();
                businessParam.setEndTime(next4Oclock);
                //日期范围
                businessParam.setRangeDate(today4Oclock + " - " + next4Oclock);
            } else {
                //分割日期参数，初始化条件
                //具体到秒
                businessParam.setStartTime(businessParam.getRangeDate().substring(0, 19));
                businessParam.setEndTime(businessParam.getRangeDate().substring(21, 41));
            }
        } else if (businessParam.getDateDimension().equals("month")) {
            //时间条件初始化
            if (StringUtils.isEmpty(businessParam.getRangeDate())) {
                businessParam.setMonth(DateUtil.getCurrentMonth());
                businessParam.setStartTime(DateUtil.getStartTimeOfCurrentMonth());
                businessParam.setEndTime(DateUtil.getEndTimeOfCurrentMonth());
            } else {
                //开始月
                String startMonth=businessParam.getRangeDate().substring(0,7);
                //结束月
                String endMonth=businessParam.getRangeDate().substring(10,17);

                businessParam.setStartTime(DateUtil.getStartTimeOfMonth(startMonth));
                businessParam.setEndTime(DateUtil.getEndTimeOfMonth(endMonth));
            }
        }


        LayuiPageInfo pageBySpec = this.businessService.findPageBySpec(businessParam);
        return pageBySpec;
    }
}
